package com.supermali.behavior.glide;

import com.supermali.creater.GameStarter;
import com.supermali.entity.MapImageAbstract;
import com.supermali.entity.npc.IntesectDirect;
import com.supermali.entity.npc.finish.FinishAbstract;
import com.supermali.entity.npc.finish.FlagPoleFinish;
import com.supermali.entity.npc.finish.WhiteFlagFinish;
import com.supermali.entity.npc.person.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class DefaultGlideBehavior extends GlideBehavior {

    private MapImageAbstract mapImageAbstract;

    private double vx =0;

    private double vy = 1d/50;

    private boolean isOver =true;

    private boolean finish = false;

    public DefaultGlideBehavior(MapImageAbstract mapImageAbstract) {
        this.mapImageAbstract = mapImageAbstract;
    }

    @Override
    public void glide(long delta) {
        if(!isOver) {
            GameStarter gameStarter = mapImageAbstract.getMapCreater().getGameStarter();
            if(gameStarter.getGameStatu()== GameStarter.GameStatu.Running) {
                gameStarter.setGameStatu(GameStarter.GameStatu.PreTips);
            }
            double dy = vy * delta;
            double ynew = mapImageAbstract.getY() - dy;
            mapImageAbstract.setY(ynew);
            Set<IntesectDirect> intesectDirects = null;
            if(mapImageAbstract instanceof Person){
                intesectDirects = ((Person) mapImageAbstract).checkCollide();
            }else if(mapImageAbstract instanceof WhiteFlagFinish){
                intesectDirects = ((WhiteFlagFinish) mapImageAbstract).checkCollide();
            }
            boolean shouldOver = shouldOver(intesectDirects);
            if(shouldOver){
                if(gameStarter.getGameStatu()== GameStarter.GameStatu.PreTips) {
                    gameStarter.setGameStatu(GameStarter.GameStatu.FinishMoveback);
                }
                isOver=true;
                finish=true;
                if(mapImageAbstract instanceof Person){
                    ((Person) mapImageAbstract).setRunState(Person.RunState.FORWARD);
                }
            }
        }
    }

    private void dealFinish(FinishAbstract finishAbstract,Person person){
        if(finishAbstract instanceof WhiteFlagFinish){
            person.setRunState(Person.RunState.SITDOWN);

        }else if(finishAbstract instanceof FlagPoleFinish){
            person.setRunState(Person.RunState.HUG);
        }
    }

    /**
     * 校验是否应该结束滑动
     * */
    private boolean shouldOver(Set<IntesectDirect> intesectDirects){
        if(intesectDirects ==null) return false;
        List<IntesectDirect> list = new ArrayList<>();
        for(IntesectDirect direct: intesectDirects){
            MapImageAbstract intesect = direct.getIntesect();
            if(!(intesect instanceof FinishAbstract)){
                list.add(direct);
            }
        }
        for(IntesectDirect direct: list){
            if(direct.getDirect()== IntesectDirect.Direct.TOP){
                return true;
            }
        }
        return false;
    }

    @Override
    public void startGlide(FinishAbstract finishAbstract){
        this.isOver = false;
        if(mapImageAbstract instanceof Person){
            dealFinish(finishAbstract, (Person) mapImageAbstract);
        }
    }

    @Override
    public  boolean isFinish() {
        return finish;
    }

    @Override
    public boolean isOver() {
        return isOver;
    }


}
